package cn.crism.chatme.controller;

import cn.crism.chatme.enums.OperatorFriendRequestTypeEnum;
import cn.crism.chatme.service.FriendService;
import cn.crism.chatme.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @CreateDate: 2018/12/27 2:59 PM
 * @Description:
 * @Author: Crism
 */
@RestController
@RequestMapping("f")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @PostMapping("/queryMyFriends")
    public JSONResult queryMyFriends(@RequestParam("userId") String userId){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("参数不能为空！");
        }
        /* 查询好友请求 */
        return JSONResult.ok(friendService.queryMyFriends(userId));
    }

    @PostMapping("/queryFriendRequest")
    public JSONResult queryFriendReques(@RequestParam("userId") String userId){

        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("参数不能为空！");
        }
        /* 查询好友请求 */
        return JSONResult.ok(friendService.queryFriendRequestList(userId));
    }

    /* 接受或者忽略朋友请求 */
    @PostMapping("/operatorFriendRequest")
    public JSONResult operatorFriendRequest(@RequestParam("accessUserId") String accessUserId,
                                            @RequestParam("sendUserId") String sendUserId,
                                            @RequestParam("opType") Integer opType){

        if (StringUtils.isBlank(accessUserId) || StringUtils.isBlank(sendUserId) || opType == null){
            return JSONResult.errorMsg("参数不能为空！");
        }

        if (StringUtils.isBlank(OperatorFriendRequestTypeEnum.getMsgByType(opType))){
            return JSONResult.errorMsg("参数错误！");
        }

        if (opType == OperatorFriendRequestTypeEnum.IGNORE.type){
            /* 删除数据库的好友请求 */
            friendService.deleteFriendRequest(accessUserId,sendUserId);
        } else if (opType == OperatorFriendRequestTypeEnum.PASS.type){
            /* 数据库新增加两条好友关系记录 */
            /* 删除数据库的好友请求 */
            friendService.passFriendRequest(accessUserId,sendUserId);

        }


        /* 查询好友请求 */
        return JSONResult.ok(friendService.queryMyFriends(accessUserId));
    }
}
